﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonControls.MarkdownEditor;
using DevelopTool.Forms.CommonHelpers;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Forms.Properties;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using Sunny.UI;

namespace DevelopTool.Forms
{
    /// <summary>
    /// 关于
    /// </summary>
    public partial class AboutForm : BaseUIForm
    {

        private List<ManageMenu> manageMenus;

        private MarkdownEditorControl markdownEditorControl;

        public AboutForm()
        {
            InitializeComponent();
        }

        private void AboutForm_Load(object sender,EventArgs e)
        {
            this.Text += "-" + GlobalData.AppName_zh;

            this.uiScrollingText.FillColor = Setting.Theme.BackColor;
            this.uiScrollingText.ForeColor = Setting.Theme.ForeColor;
            this.uiScrollingText.RectColor = Setting.Theme.BackColor;

            this.uiScrollingText.Text = $"欢迎使用 {GlobalData.AppName_zh} V{Application.ProductVersion}";

            LoadingHelper.ShowLoading("正在准备文档",() =>
            {
               // this.manageMenus = ZipHelper.ExtractToManageMenu(Resources.docs);
                this.manageMenus = ZipHelper.ExtractToManageMenu(Resources.docs, GlobalData.AppTempDirectory);
            });

            #region 删除最外面的目录
            this.manageMenus.RemoveAll(it => it.Id == 1);
            this.manageMenus = this.manageMenus.Select(it =>
            {
                ManageMenu manageMenu = it;
                if(manageMenu.NodePid == 1) manageMenu.NodePid = 0;

                return manageMenu;
            }).ToList();
            #endregion 删除最外面的目录

            this.manageControl.SetMenuData(this.manageMenus,"操作手册",362938);

            foreach(var menu in this.manageMenus)
            {
                SystemMenuModel systemMenu = SystemMenu.GetMenuByText(menu.NodeName.Replace(".md",""),SystemMenu.List);

                if(systemMenu != null && systemMenu.Symbol > 0)
                    this.manageControl.SetSymbol(menu,systemMenu.Symbol);
            }


            //添加Markdown预览控件
            this.markdownEditorControl = new MarkdownEditorControl()
            {
                ShowEditorControl = false,
                ShowCodeHighlightingBtn = false,
            };
            this.manageControl.ShowControl(markdownEditorControl);           

            ThemHelper.SetSystemThem(this);            
        }


        private void ShowOper(ManageMenu manageMenu)
        {
            if(manageMenu == null)
            {
                return;
               // this.markdownEditorControl.FileContent = string.Empty;
            }
            else
            {
                this.markdownEditorControl.FileContent = manageMenu.Remark;
                this.markdownEditorControl.Text = manageMenu.NodeName;

                this.manageControl.SelectNode(manageMenu);
            }
        }


        public void ShowOper(string operName)
        {
            if(string.IsNullOrEmpty(operName)) return;

            ManageMenu manageMenu = this.manageMenus.FirstOrDefault(it => it.NodeName == operName + ".md");

            ShowOper(manageMenu);
        }

        private void manageControl_MenuClick(object sender,MenuEventArgs e)
        {
            if(e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                ShowOper(e.ManageMenu);
            }
        }

        private void manageControl_MenuQuery(object sender,string text)
        {
            Dictionary<string,List<ManageMenu>> dic = new Dictionary<string,List<ManageMenu>>();

            var list1 = this.manageMenus.FindAll(it => it.NodeType == MenuNodeType.Node && !string.IsNullOrEmpty(it.NodeName) && it.NodeName.IndexOf(text,StringComparison.OrdinalIgnoreCase) > -1).ToList();
            dic.Add("关联到的文档名称",list1);

            var list2 = this.manageMenus.FindAll(it => it.NodeType == MenuNodeType.Node && !string.IsNullOrEmpty(it.Remark) && MarkdownHelper.MarkdownToPlainText(it.Remark).IndexOf(text,StringComparison.OrdinalIgnoreCase) > -1).ToList();
            dic.Add("关联到的内容",list2);

            this.manageControl.SetMenuQueryData(dic);
        }

        private void manageControl_MenuDrag(object sender,MenuEventArgs e)
        {
            e.Cancel = true;
        }

        #region 实现鼠标事件穿透效果

        // 导入 Windows API 函数
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,int Msg,int wParam,int lParam);

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

        // 鼠标消息的常量定义
        private const int WM_NCLBUTTONDOWN = 0xA1;  // 非客户区鼠标按下消息
        private const int HT_CAPTION = 0x2;         // 标题栏区域

        private void uiScrollingText_MouseDown(object sender,MouseEventArgs e)
        {
            // 检查是否按下了左键
            if(e.Button == MouseButtons.Left)
            {
                // 释放鼠标捕获
                ReleaseCapture();

                // 向窗体发送 WM_NCLBUTTONDOWN 消息，HT_CAPTION 作为 wParam
                SendMessage(Handle,WM_NCLBUTTONDOWN,HT_CAPTION,0);
            }
        }

        #endregion 实现鼠标事件穿透效果

    }
}
